package com.hui.controller;

import com.hui.entity.Resource;
import com.hui.entity.RoleResource;
import com.hui.entity.User;
import com.hui.entity.UserRole;
import com.hui.service.ResourceService;
import com.hui.service.RoleResourceService;
import com.hui.service.UserRoleService;
import com.hui.util.CommonHelper;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.*;

@Controller
@RequestMapping("/admin")
public class IndexController {
    @Autowired
    private ResourceService resourceService;
    @Autowired
    private UserRoleService userRoleService;
    @Autowired
    private RoleResourceService roleResourceService;

    @RequestMapping("/index")
    public String index(HttpServletRequest request, HttpServletResponse response) {
        //生成对应的菜单栏目
        //1.根据用户id找到用户roldId 表sys_user_role
        Subject currentUser = SecurityUtils.getSubject();
        User user = (User) currentUser.getPrincipals().getPrimaryPrincipal();
        UserRole ur =userRoleService.getUserRoleById(user.getId());
        Integer roleId =ur.getRoleId();
        //2.根据角色获取到权限 roleId 找到 所有的resource
//            List<RoleResource> list =roleResourceService.getResourcesById(roleId);
        //3.创建菜单树
//            for()
        String tree ="<i-Menu :theme=\"theme2\">\n" +
                "                <Submenu name=\"1\">\n" +
                "                    <template slot=\"title\">\n" +
                "                        <Icon type=\"ios-paper\"></Icon>\n" +
                "                        {{hello}}\n" +
                "                    </template>\n" +
                "                    <router-link to=\"/welcome\">\n" +
                "                        <Menu-Item name=\"1-1\">欢迎111</Menu-Item>\n" +
                "                    </router-link>\n" +
                "                    <Menu-Item name=\"1-2\">评论管理</Menu-Item>\n" +
                "                    <Menu-Item name=\"1-3\">举报管理</Menu-Item>\n" +
                "                </Submenu>\n" +
                "                <Submenu name=\"2\">\n" +
                "                    <template slot=\"title\">\n" +
                "                        <Icon type=\"ios-people\"></Icon>\n" +
                "                        用户管理\n" +
                "                    </template>\n" +
                "                    <Menu-Item name=\"2-1\">新增用户</Menu-Item>\n" +
                "                    <Menu-Item name=\"2-2\">活跃用户</Menu-Item>\n" +
                "                </Submenu>\n" +
                "                <Submenu name=\"3\">\n" +
                "                    <template slot=\"title\">\n" +
                "                        <Icon type=\"stats-bars\"></Icon>\n" +
                "                        统计分析\n" +
                "                    </template>\n" +
                "                    <Menu-Group title=\"使用\">\n" +
                "                        <Menu-Item name=\"3-1\">新增和启动</Menu-Item>\n" +
                "                        <Menu-Item name=\"3-2\">活跃分析</Menu-Item>\n" +
                "                        <Menu-Item name=\"3-3\">时段分析</Menu-Item>\n" +
                "                    </Menu-Group>\n" +
                "                    <Menu-Group title=\"留存\">\n" +
                "                        <Menu-Item name=\"3-4\">用户留存</Menu-Item>\n" +
                "                        <Menu-Item name=\"3-5\">流失用户</Menu-Item>\n" +
                "                    </Menu-Group>\n" +
                "                </Submenu>\n" +
                "            </i-Menu>";
        request.setAttribute("tree",tree);
        return "admin/mainInfo";
    }

    @RequestMapping(value = "test", method = RequestMethod.GET)
    @ResponseBody
    public String test() {
        return "??";
    }
    /**
     * 用户权限路由
     * 编写成需要的json格式
     */
    @RequestMapping(value = "/meunRequest")
    @ResponseBody
    public String meunRequest(){
        //验证用户是否登录 存用户的session不一样
        Subject currentUser = SecurityUtils.getSubject();
        //获取到用户
        User user = (User) currentUser.getPrincipals().getPrimaryPrincipal();
        List<Resource> resourceList =resourceService.getUserAllResource(user.getUsername());
        //生成对应的router
        List routerList =new ArrayList();
        for(Resource rs:resourceList){
            Map map =new HashMap();
            map.put("path",rs.getUrl());
            map.put("name",rs.getName());
            map.put("component",rs.getOutUrl());
            routerList.add(map);
        }
        return CommonHelper.toJson(routerList);
    }
}
